package erfgame.frontend.desktop.view.world.list;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import erfgame.core.world.World;

import erfgame.frontend.desktop.view.AbstractView;

public class ListWorldsView extends AbstractView {
	
	private static final String RESOURCES = "bundles.ListWorldsView";
	
	public static final int ACTION_TYPE_WORLD_DELETE   = 2;
	public static final int ACTION_TYPE_WORLD_SELECTED = 1;
	public static final int ACTION_TYPE_CANCEL 		   = 0;
	
	private JButton cancelButton;
	private JButton selectButton;
	private JButton deleteButton;
	
	private JTable worldsTable;
	
	private JPanel component;
	private List<World> worlds;
	
	public ListWorldsView() {
		
	}
	
	public void setWorlds( List<World> worlds ) {
		ResourceBundle resources = ResourceBundle.getBundle( RESOURCES );
		this.worlds = worlds;
		Object[][] rowData = new Object[ worlds.size() ][];
		for( int i=worlds.size(); i>0; ) {
			i--;
			World world = worlds.get( i );
			rowData[i] = new Object[]{
					world.getWorldName(), 
					world.getBlocksX(), 
					world.getBlocksY(), 
					world.getBlocksZ()
			};
		}
		String[] columnNames = new String[] {
				resources.getString( "world_name" ), 
				resources.getString( "world_width" ),
				resources.getString( "world_height" ), 
				resources.getString( "world_depth" )
			};

		DefaultTableModel model = new DefaultTableModel( rowData, columnNames ) {
			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		
		this.worldsTable.setModel(model);
	}
	 
	public void init() {
		
		ResourceBundle resources = ResourceBundle.getBundle( RESOURCES );
		
		this.worldsTable = new JTable();
		this.worldsTable.setColumnSelectionAllowed( false );
		this.worldsTable.setDragEnabled( false );
		this.worldsTable.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
		this.worldsTable.getSelectionModel().addListSelectionListener(
				new ListSelectionListener() {
					public void valueChanged(ListSelectionEvent e) {
						int row = worldsTable.getSelectedRow();
						deleteButton.setEnabled( row >= 0 );
						selectButton.setEnabled( row >= 0 );
					}
				}
		);
		
		this.component = new JPanel( new GridBagLayout() );
		
		JScrollPane scrollPane = new JScrollPane( this.worldsTable );
		scrollPane.setPreferredSize( new Dimension( 320, 240 ) );
		
		GridBagConstraints tableConstraints = new GridBagConstraints();
		tableConstraints.gridwidth = GridBagConstraints.REMAINDER;
		tableConstraints.fill = GridBagConstraints.BOTH;
		
		this.component.add( scrollPane, tableConstraints );
		
		JPanel buttonsPanel = new JPanel( new BorderLayout() );
		
		this.cancelButton = new JButton( resources.getString( "cancel" ) );
		this.cancelButton.addActionListener( this.createFireViewActionListener( ACTION_TYPE_CANCEL ) );
		
		this.selectButton = new JButton( resources.getString( "select" ) );
		this.selectButton.setEnabled(false);
		this.selectButton.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int row = worldsTable.getSelectedRow();
				if( row >= 0 ) {
					World world = worlds.get( row );
					fireViewAction( ACTION_TYPE_WORLD_SELECTED, world );
				}
			}
		});

		this.deleteButton = new JButton( resources.getString("delete") );
		this.deleteButton.setEnabled(false);
		this.deleteButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int row = worldsTable.getSelectedRow();
				if( row >= 0 ) {
					World world = worlds.get( row );
					fireViewAction( ACTION_TYPE_WORLD_DELETE, world );
				}
			}
		});
		
		JPanel actionPanel = new JPanel( new GridLayout(1, 2, 5, 5) );
		
		actionPanel.add( this.selectButton );
		actionPanel.add( this.deleteButton );
		
		buttonsPanel.add( actionPanel, BorderLayout.WEST );
		buttonsPanel.add( this.cancelButton, BorderLayout.EAST );
		
		GridBagConstraints buttonsConstraints = new GridBagConstraints();
		buttonsConstraints.gridwidth = GridBagConstraints.REMAINDER;
		buttonsConstraints.fill = GridBagConstraints.HORIZONTAL;

		this.component.add( buttonsPanel, buttonsConstraints );
	}
	
	public void destroy() {
		this.component = null;
		this.selectButton = null;
		this.cancelButton = null;
		this.worldsTable = null;
	}
	
	public JComponent getComponent() {
		return this.component;
	}
	
}
